home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / LIB / GLUT / glut_cmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  11.8 KB  |  395 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994, 1996, 1997.  */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #if !defined(_WIN32)
  12. #include <X11/Xlib.h>
  13. #include <X11/Xutil.h>
  14. #include <X11/Xatom.h>  /* for XA_RGB_DEFAULT_MAP atom */
  15. #if defined(__vms)
  16. #include <Xmu/StdCmap.h>  /* for XmuLookupStandardColormap */
  17. #else
  18. #include <X11/Xmu/StdCmap.h>  /* for XmuLookupStandardColormap */
  19. #endif
  20. #endif
  21.  
  22. /* SGI optimization introduced in IRIX 6.3 to avoid X server
  23.    round trips for interning common X atoms. */
  24. #if defined(_SGI_EXTRA_PREDEFINES) && !defined(NO_FAST_ATOMS)
  25. #include <X11/SGIFastAtom.h>
  26. #else
  27. #define XSGIFastInternAtom(dpy,string,fast_name,how) XInternAtom(dpy,string,how)
  28. #endif
  29.  
  30. #include "glutint.h"
  31. #include "layerutil.h"
  32.  
  33. GLUTcolormap *__glutColormapList = NULL;
  34.  
  35. GLUTcolormap *
  36. __glutAssociateNewColormap(XVisualInfo * vis)
  37. {
  38.   GLUTcolormap *cmap;
  39.   int transparentPixel, i;
  40.   unsigned long pixels[255];
  41.  
  42.   cmap = (GLUTcolormap *) malloc(sizeof(GLUTcolormap));
  43.   if (!cmap)
  44.     __glutFatalError("out of memory.");
  45. #if defined(_WIN32)
  46.   pixels[0] = 0;        /* avoid compilation warnings on win32 */
  47.   cmap->visual = 0;
  48.   cmap->size = 256;     /* always assume 256 on Win32 */
  49. #else
  50.   cmap->visual = vis->visual;
  51.   cmap->size = vis->visual->map_entries;
  52. #endif
  53.   cmap->refcnt = 1;
  54.   cmap->cells = (GLUTcolorcell *)
  55.     malloc(sizeof(GLUTcolorcell) * cmap->size);
  56.   if (!cmap->cells)
  57.     __glutFatalError("out of memory.");
  58.   /* make all color cell entries be invalid */
  59.   for (i = cmap->size - 1; i >= 0; i--) {
  60.     cmap->cells[i].component[GLUT_RED] = -1.0;
  61.     cmap->cells[i].component[GLUT_GREEN] = -1.0;
  62.     cmap->cells[i].component[GLUT_BLUE] = -1.0;
  63.   }
  64.   transparentPixel = __glutGetTransparentPixel(__glutDisplay, vis);
  65.   if (transparentPixel == -1 || transparentPixel >= cmap->size) {
  66.  
  67.     /* If there is no transparent pixel or if the transparent
  68.        pixel is outside the range of valid colormap cells (HP
  69.        can implement their overlays this smart way since their
  70.        transparent pixel is 255), we can AllocAll the colormap.
  71.        See note below.  */
  72.  
  73.     cmap->cmap = XCreateColormap(__glutDisplay,
  74.       __glutRoot, cmap->visual, AllocAll);
  75.   } else {
  76.  
  77.     /* On machines where zero (or some other value in the range
  78.        of 0 through map_entries-1), BadAlloc may be generated
  79.        when an AllocAll overlay colormap is allocated since the
  80.        transparent pixel precludes all the cells in the colormap
  81.        being allocated (the transparent pixel is pre-allocated).
  82.        So in this case, use XAllocColorCells to allocate
  83.        map_entries-1 pixels (that is, all but the transparent
  84.        pixel.  */
  85.  
  86. #if defined(_WIN32)
  87.     cmap->cmap = XCreateColormap(__glutDisplay,
  88.       __glutRoot, 0, AllocNone);
  89. #else
  90.     cmap->cmap = XCreateColormap(__glutDisplay,
  91.       __glutRoot, vis->visual, AllocNone);
  92.     XAllocColorCells(__glutDisplay, cmap->cmap, False, 0, 0,
  93.       pixels, cmap->size - 1);
  94. #endif
  95.   }
  96.   cmap->next = __glutColormapList;
  97.   __glutColormapList = cmap;
  98.   return cmap;
  99. }
  100.  
  101. static GLUTcolormap *
  102. associateColormap(XVisualInfo * vis)
  103. {
  104. #if !defined(_WIN32)
  105.   GLUTcolormap *cmap = __glutColormapList;
  106.  
  107.   while (cmap != NULL) {
  108.     /* Play safe: compare visual IDs, not Visual*'s. */
  109.     if (cmap->visual->visualid == vis->visual->visualid) {
  110.       /* Already have created colormap for the visual. */
  111.       cmap->refcnt++;
  112.       return cmap;
  113.     }
  114.     cmap = cmap->next;
  115.   }
  116. #endif
  117.   return __glutAssociateNewColormap(vis);
  118. }
  119.  
  120. void
  121. __glutSetupColormap(XVisualInfo * vi, GLUTcolormap ** colormap, Colormap * cmap)
  122. {
  123. #if defined(_WIN32)
  124.   if (vi->dwFlags & PFD_NEED_PALETTE || vi->iPixelType == PFD_TYPE_COLORINDEX) {
  125.     *colormap = associateColormap(vi);
  126.     *cmap = (*colormap)->cmap;
  127.   } else {
  128.     *colormap = NULL;
  129.     *cmap = 0;
  130.   }
  131. #else
  132.   Status status;
  133.   XStandardColormap *standardCmaps;
  134.   int i, numCmaps;
  135.   static Atom hpColorRecoveryAtom = -1;
  136.   int isRGB, visualClass, rc;
  137.  
  138. #if defined(__cplusplus) || defined(c_plusplus)
  139.   visualClass = vi->c_class;
  140. #else
  141.   visualClass = vi->class;
  142. #endif
  143.   switch (visualClass) {
  144.   case PseudoColor:
  145.     /* Mesa might return a PseudoColor visual for RGB mode. */
  146.     rc = glXGetConfig(__glutDisplay, vi, GLX_RGBA, &isRGB);
  147.     if (rc == 0 && isRGB) {
  148.       /* Must be Mesa. */
  149.       *colormap = NULL;
  150.       if (MaxCmapsOfScreen(DefaultScreenOfDisplay(__glutDisplay)) == 1
  151.         && vi->visual == DefaultVisual(__glutDisplay, __glutScreen)) {
  152.         char *privateCmap = getenv("MESA_PRIVATE_CMAP");
  153.  
  154.         if (privateCmap) {
  155.           /* User doesn't want to share colormaps. */
  156.           *cmap = XCreateColormap(__glutDisplay, __glutRoot,
  157.             vi->visual, AllocNone);
  158.         } else {
  159.           /* Share the root colormap. */
  160.           *cmap = DefaultColormap(__glutDisplay, __glutScreen);
  161.         }
  162.       } else {
  163.         /* Get our own PseudoColor colormap. */
  164.         *cmap = XCreateColormap(__glutDisplay, __glutRoot,
  165.           vi->visual, AllocNone);
  166.       }
  167.     } else {
  168.       /* CI mode, real GLX never returns a PseudoColor visual
  169.          for RGB mode. */
  170.       *colormap = associateColormap(vi);
  171.       *cmap = (*colormap)->cmap;
  172.     }
  173.     break;
  174.   case TrueColor:
  175.   case DirectColor:
  176.     *colormap = NULL;   /* NULL if RGBA */
  177.  
  178.     /* Hewlett-Packard supports a feature called "HP Color
  179.        Recovery". Mesa has code to use HP Color Recovery.  For
  180.        Mesa to use this feature, the atom
  181.        _HP_RGB_SMOOTH_MAP_LIST must be defined on the root
  182.        window AND the colormap obtainable by XGetRGBColormaps
  183.        for that atom must be set on the window.  If that
  184.        colormap is not set, the output will look stripy. */
  185.  
  186.     if (hpColorRecoveryAtom == -1) {
  187.       char *xvendor;
  188.  
  189. #define VENDOR_HP "Hewlett-Packard"
  190.  
  191.       /* Only makes sense to make XInternAtom round-trip if we
  192.          know that we are connected to an HP X server. */
  193.       xvendor = ServerVendor(__glutDisplay);
  194.       if (!strncmp(xvendor, VENDOR_HP, sizeof(VENDOR_HP) - 1)) {
  195.         hpColorRecoveryAtom = XInternAtom(__glutDisplay, "_HP_RGB_SMOOTH_MAP_LIST", True);
  196.       } else {
  197.         hpColorRecoveryAtom = None;
  198.       }
  199.     }
  200.     if (hpColorRecoveryAtom != None) {
  201.       status = XGetRGBColormaps(__glutDisplay, __glutRoot,
  202.         &standardCmaps, &numCmaps, hpColorRecoveryAtom);
  203.       if (status == 1) {
  204.         for (i = 0; i < numCmaps; i++) {
  205.           if (standardCmaps[i].visualid == vi->visualid) {
  206.             *cmap = standardCmaps[i].colormap;
  207.             XFree(standardCmaps);
  208.             return;
  209.           }
  210.         }
  211.         XFree(standardCmaps);
  212.       }
  213.     }
  214. #ifndef SOLARIS_2_4_BUG
  215.     /* Solaris 2.4 and 2.5 have a bug in their
  216.        XmuLookupStandardColormap implementations.  Please
  217.        compile your Solaris 2.4 or 2.5 version of GLUT with
  218.        -DSOLARIS_2_4_BUG to work around this bug. The symptom
  219.        of the bug is that programs will get a BadMatch error
  220.        from X_CreateWindow when creating a GLUT window because
  221.        Solaris 2.4 and 2.5 create a  corrupted RGB_DEFAULT_MAP
  222.        property.  Note that this workaround prevents Colormap
  223.        sharing between applications, perhaps leading
  224.        unnecessary colormap installations or colormap flashing.
  225.        Sun fixed this bug in Solaris 2.6. */
  226.     status = XmuLookupStandardColormap(__glutDisplay,
  227.       vi->screen, vi->visualid, vi->depth, XA_RGB_DEFAULT_MAP,
  228.       /* replace */ False, /* retain */ True);
  229.     if (status == 1) {
  230.       status = XGetRGBColormaps(__glutDisplay, __glutRoot,
  231.         &standardCmaps, &numCmaps, XA_RGB_DEFAULT_MAP);
  232.       if (status == 1) {
  233.         for (i = 0; i < numCmaps; i++) {
  234.           if (standardCmaps[i].visualid == vi->visualid) {
  235.             *cmap = standardCmaps[i].colormap;
  236.             XFree(standardCmaps);
  237.             return;
  238.           }
  239.         }
  240.         XFree(standardCmaps);
  241.       }
  242.     }
  243. #endif
  244.     /* If no standard colormap but TrueColor, just make a
  245.        private one. */
  246.     /* XXX Should do a better job of internal sharing for
  247.        privately allocated TrueColor colormaps. */
  248.     /* XXX DirectColor probably needs ramps hand initialized! */
  249.     *cmap = XCreateColormap(__glutDisplay, __glutRoot,
  250.       vi->visual, AllocNone);
  251.     break;
  252.   case StaticColor:
  253.   case StaticGray:
  254.   case GrayScale:
  255.     /* Mesa supports these visuals */
  256.     *colormap = NULL;
  257.     *cmap = XCreateColormap(__glutDisplay, __glutRoot,
  258.       vi->visual, AllocNone);
  259.     break;
  260.   default:
  261.     __glutFatalError(
  262.       "could not allocate colormap for visual type: %d.",
  263.       visualClass);
  264.   }
  265.   return;
  266. #endif
  267. }
  268.  
  269. #if !defined(_WIN32)
  270. static int
  271. findColormaps(GLUTwindow * window,
  272.   Window * winlist, Colormap * cmaplist, int num, int max)
  273. {
  274.   GLUTwindow *child;
  275.   int i;
  276.  
  277.   /* Do not allow more entries that maximum number of
  278.      colormaps! */
  279.   if (num >= max)
  280.     return num;
  281.   /* Is cmap for this window already on the list? */
  282.   for (i = 0; i < num; i++) {
  283.     if (cmaplist[i] == window->cmap)
  284.       goto normalColormapAlreadyListed;
  285.   }
  286.   /* Not found on the list; add colormap and window. */
  287.   winlist[num] = window->win;
  288.   cmaplist[num] = window->cmap;
  289.   num++;
  290.  
  291. normalColormapAlreadyListed:
  292.  
  293.   /* Repeat above but for the overlay colormap if there one. */
  294.   if (window->overlay) {
  295.     if (num >= max)
  296.       return num;
  297.     for (i = 0; i < num; i++) {
  298.       if (cmaplist[i] == window->overlay->cmap)
  299.         goto overlayColormapAlreadyListed;
  300.     }
  301.     winlist[num] = window->overlay->win;
  302.     cmaplist[num] = window->overlay->cmap;
  303.     num++;
  304.   }
  305. overlayColormapAlreadyListed:
  306.  
  307.   /* Recursively search children. */
  308.   child = window->children;
  309.   while (child) {
  310.     num = findColormaps(child, winlist, cmaplist, num, max);
  311.     child = child->siblings;
  312.   }
  313.   return num;
  314. }
  315.  
  316. void
  317. __glutEstablishColormapsProperty(GLUTwindow * window)
  318. {
  319.   /* this routine is strictly X.  Win32 doesn't need to do
  320.      anything of this sort (but has to do other wacky stuff
  321.      later). */
  322.   static Atom wmColormapWindows = None;
  323.   Window *winlist;
  324.   Colormap *cmaplist;
  325.   Status status;
  326.   int maxcmaps, num;
  327.  
  328.   assert(!window->parent);
  329.   maxcmaps = MaxCmapsOfScreen(ScreenOfDisplay(__glutDisplay,
  330.       __glutScreen));
  331.   /* For portability reasons we don't use alloca for winlist
  332.      and cmaplist, but we could. */
  333.   winlist = (Window *) malloc(maxcmaps * sizeof(Window));
  334.   cmaplist = (Colormap *) malloc(maxcmaps * sizeof(Colormap));
  335.   num = findColormaps(window, winlist, cmaplist, 0, maxcmaps);
  336.   if (num < 2) {
  337.     /* Property no longer needed; remove it. */
  338.     wmColormapWindows = XSGIFastInternAtom(__glutDisplay,
  339.       "WM_COLORMAP_WINDOWS", SGI_XA_WM_COLORMAP_WINDOWS, False);
  340.     if (wmColormapWindows == None) {
  341.       __glutWarning("Could not intern X atom for WM_COLORMAP_WINDOWS.");
  342.       return;
  343.     }
  344.     XDeleteProperty(__glutDisplay, window->win, wmColormapWindows);
  345.   } else {
  346.     status = XSetWMColormapWindows(__glutDisplay, window->win,
  347.       winlist, num);
  348.     /* XSetWMColormapWindows should always work unless the
  349.        WM_COLORMAP_WINDOWS property cannot be intern'ed.  We
  350.        check to be safe. */
  351.     if (status == False)
  352.       __glutFatalError("XSetWMColormapWindows returned False.");
  353.   }
  354.   /* For portability reasons we don't use alloca for winlist
  355.      and cmaplist, but we could. */
  356.   free(winlist);
  357.   free(cmaplist);
  358. }
  359.  
  360. GLUTwindow *
  361. __glutToplevelOf(GLUTwindow * window)
  362. {
  363.   while (window->parent) {
  364.     window = window->parent;
  365.   }
  366.   return window;
  367. }
  368. #endif
  369.  
  370. void
  371. __glutFreeColormap(GLUTcolormap * cmap)
  372. {
  373.   GLUTcolormap *cur, **prev;
  374.  
  375.   cmap->refcnt--;
  376.   if (cmap->refcnt == 0) {
  377.     /* remove from colormap list */
  378.     cur = __glutColormapList;
  379.     prev = &__glutColormapList;
  380.     while (cur) {
  381.       if (cur == cmap) {
  382.         *prev = cmap->next;
  383.         break;
  384.       }
  385.       prev = &(cur->next);
  386.       cur = cur->next;
  387.     }
  388.     /* actually free colormap */
  389.     XFreeColormap(__glutDisplay, cmap->cmap);
  390.     free(cmap->cells);
  391.     free(cmap);
  392.   }
  393. }
  394.  
  395.